package com.test;

import java.io.*;

/**
 * Created by jl on 2016/8/7.
 */
public class Demo1 {
    private static long now = System.currentTimeMillis();
    private static final int COUNT_PER_COMMIT = 100;//每5000条提交一次
    public static void main(String[] args) throws IOException {
        File file = new File("C:\\Users\\Administrator\\Desktop\\201602.csv");
        File outFile = new File("C:\\Users\\Administrator\\Desktop\\201602.sql");
        InputStreamReader reader = new InputStreamReader(new FileInputStream(file), "gbk");
        BufferedReader bufferedReader = new BufferedReader(reader);
        String lineTxt;
        int rowNum = 1;
        String title = bufferedReader.readLine();
        StringBuilder sb = new StringBuilder("insert all into ahebpp.T_WAP_ACTIVE_USERS values('");

        while ((lineTxt = bufferedReader.readLine()) != null) {
            String[] strArr = lineTxt.split(",");
            sb.append(generateParams(strArr, rowNum));
            if (rowNum % COUNT_PER_COMMIT == 0) {
                sb.append("')select 1 from dual;\ncommit;\ninsert all into ahebpp.T_WAP_ACTIVE_USERS values('");
            } else {
                sb.append("')\ninto ahebpp.T_WAP_ACTIVE_USERS values('");
            }
            rowNum++;
        }
        if ((rowNum - 1) % COUNT_PER_COMMIT == 0) {
            sb.delete(sb.length() - 50, sb.length());
        } else {
            sb.delete(sb.length() - 39, sb.length());
        }
        String sqlResult = sb.append("select 1 from dual;\ncommit;").toString();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(
                new FileOutputStream(outFile), "gbk"));
        writer.write(sqlResult);
        writer.close();
        System.out.println("ok finish");
    }

    private static String generateParams(String[] arr, int rowNum) {
        rowNum++;
        String id = now + "-" + rowNum + "";
        String productNo = arr[0];
        String lastActiveTime = arr[3];
        String area = arr[1];

        StringBuilder sb = new StringBuilder(id);
        sb.append("','");
        sb.append(productNo);
        sb.append("','");
        sb.append(lastActiveTime);
        sb.append("','");
        sb.append(area);
        return sb.toString();
    }
}
